package com.cn.util;

import org.junit.Test;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImageMarkLogo {
    public static void main(String[] args) {
        String srcImgPath = "C:\\Users\\yihanyuan\\Desktop\\1844.jpg";
        String iconPath = "C:\\Users\\yihanyuan\\Desktop\\TIM截图20190505130916.png";
        String targerPath = "C:\\Users\\yihanyuan\\Desktop\\img_mark_icon.jpg";
        String targerPath2 = "C:\\Users\\yihanyuan\\Desktop\\img_mark_icon_rotate.jpg";
        // 给图片添加水印   
//        ImageMarkLogo.markImageByIcon(iconPath, srcImgPath, targerPath, null);
        ImageMarkLogo.markByText("这是水印啊", srcImgPath, targerPath, null);
        // 给图片添加水印,水印旋转-45   
//        ImageMarkLogo.markImageByIcon(iconPath, srcImgPath, targerPath2,
//                -45);

    }

    /*
    给图片添加水印、可设置水印图片旋转角度
    @param iconPath 水印图片路径
    @param srcImgPath 源图片路径
    @param targerPath 目标图片路径
    @param degree 水印图片旋转角度
    */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 得到画笔对象   
            // Graphics g= buffImg.getGraphics(); 

            Graphics2D g = buffImg.createGraphics();

            // 设置对线段的锯齿状边缘处理   
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg
                    .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

            if (null != degree) {
                // 设置水印旋转   
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2, (double) buffImg
                                .getHeight() / 2);
            }

            // 水印图象的路径 水印一般为gif或者png的，这样可设置透明度 

            ImageIcon imgIcon = new ImageIcon(iconPath);

            // 得到Image对象。   
            Image img = imgIcon.getImage();

            float alpha = 0.5f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));
            // 表示水印图片的位置
            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);
            int len = height / 20;
            int wid = width / 100;
            int county = height / len;
            int countx = width / wid;
            int x = 0;
            int y = 0;
            for (int i = 0; i < county; i++) {
                //先添加竖的
                for (int j = 0; j < countx; j++) {
                    g.drawImage(img, x, y, null);
                    System.out.println(x + "           " + y);
                    x += len;
                }
                x = 0;
                y += wid;
            }
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            g.dispose();
            os = new FileOutputStream(targerPath);
            // 生成图片
            ImageIO.write(buffImg, "JPG", os);
            System.out.println("图片完成添加Icon印章。。。。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 给图片添加水印、可设置水印的旋转角度
     *
     * @param logoText
     * @param srcImgPath
     * @param targerPath
     * @param degree
     */
    public static void markByText(String logoText, String srcImgPath,
                                  String targerPath, Integer degree) {
        // 主图片的路径
        InputStream is = null;
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 得到画笔对象
            // Graphics g= buffImg.getGraphics();
            Graphics2D g = buffImg.createGraphics();

            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg
                    .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2, (double) buffImg
                                .getHeight() / 2);
            }

            // 设置颜色
            g.setColor(Color.red);

            // 设置 Font
            g.setFont(new Font("宋体", Font.BOLD, 30));

            float alpha = 0.5f;
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y) .
            FontMetrics metrics = g.getFontMetrics(g.getFont());
            int textH = metrics.getHeight(); //字符串的高, 只和字体有关
            int textW = metrics.stringWidth(logoText); //字符串的宽
            //再加一个文本长度可以使全部充满
            int width = srcImg.getWidth(null) + textW;
            int height = srcImg.getHeight(null) + textH;
            //直接统计下，y轴可以重复次数
            int countx = width / textW + 1;
            int county = height / textH + 1;
            int count = 0;
            for (int i = 0; i <= countx; i++) {
                //先添加竖的
                for (int j = 0; j <= county; j++) {
                    g.drawString(logoText, i * textW + 0, j * textH + 0);
                    System.out.println(i * textW + 10 + "       " + j * textH + 10);
                    count++;
                }
            }
            System.out.println(count);
            g.dispose();

            os = new FileOutputStream(targerPath);

            // 生成图片
            ImageIO.write(buffImg, "JPG", os);

            System.out.println("图片完成添加文字印章。。。。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    public void main1() {
        String txt = "\"0000012a15e249c78375d8cb022434d9\"\t\"green\"\t\"\"\t\"查询个人积分信息\"\t\"{\"\"accountCode\"\":\"\"031012001030018879\"\",\"\"tranTime\"\":1555640825804,\"\"encryptData\"\":\"\"d4781a93583bb4d0f97541a983fb65f6\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/4/22 19:08:20\"\t\"1\"\t\"2019/4/22 19:08:20\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"0000032fadad42c7b3c79a8c3453ab8b\"\t\"reclaim\"\t\"成功\"\t\"查找两网小区\"\t\"{\"\"head\"\":{\"\"reqCode\"\":\"\"531fa308460a407bb0edcee680eec8ea\"\",\"\"reqTime\"\":\"\"20190402181116\"\",\"\"version\"\":\"\"1\"\"},\"\"sign\"\":\"\"4ec2606a874bc12ea5a18d9494be5e7b\"\",\"\"body\"\":\"\"{\\\"\"parentCode\\\"\":\\\"\"310118000000\\\"\",\\\"\"grade\\\"\":\\\"\"2\\\"\"}\"\"}\"\t\"1\"\t\"2019/4/2 18:11:17\"\t\"1\"\t\"2019/4/2 18:11:17\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"000006b525ae427d9e9f0372444713db\"\t\"green\"\t\"\"\t\"查询十条积分\"\t\"{\"\"accountCode\"\":\"\"131011301212633066\"\",\"\"tranTime\"\":1553752339982,\"\"encryptData\"\":\"\"8e5851839a704b2b109d004da0c56ac4\"\",\"\"pageSize\"\":10,\"\"app_id\"\":\"\"M1523497402120\"\",\"\"pageNum\"\":1}\"\t\"1\"\t\"2019/4/9 10:31:13\"\t\"1\"\t\"2019/4/9 10:31:13\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"000013252ba64668be025cbb32e3f428\"\t\"green\"\t\"\"\t\"实名认证接口\"\t\"{\"\"cardNum\"\":\"\"8pwolwqoMf9GmWfkNTv6THXiZH/bHOQ1mCnAjUAqCVI=\"\",\"\"accountCode\"\":\"\"131011001716291805\"\",\"\"tranTime\"\":1555640826286,\"\"cardName\"\":\"\"3pfw3EfG3OoVwdpwOed5CQ==\"\",\"\"encryptData\"\":\"\"4e6578d5170a0c76e06d24657b5d089a\"\",\"\"phoneNum\"\":\"\"WSUTO5mXVXMRZkivhY+lcA==\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/5/6 19:05:06\"\t\"1\"\t\"2019/5/6 19:05:06\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00004146202345ea8094acecf07f7721\"\t\"green\"\t\"\"\t\"获取用户实名制信息\"\t\"{\"\"accountCode\"\":\"\"131011801680811072\"\",\"\"tranTime\"\":1547784250461,\"\"encryptData\"\":\"\"bf89ba9eef747ca906c2e4a7b1f0d9a0\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/1/20 18:17:40\"\t\"1\"\t\"2019/1/20 18:17:40\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"0000488f138b417492c609350dcd38dc\"\t\"green\"\t\"\"\t\"查询十条积分\"\t\"{\"\"accountCode\"\":\"\"131011301137361517\"\",\"\"tranTime\"\":1547784250003,\"\"encryptData\"\":\"\"500d880673dc7e43b1d780750e7c03ce\"\",\"\"pageSize\"\":10,\"\"app_id\"\":\"\"M1523497402120\"\",\"\"pageNum\"\":1}\"\t\"1\"\t\"2019/1/24 15:30:32\"\t\"1\"\t\"2019/1/24 15:30:32\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00006c1cafcb482db193df5c209a4e3d\"\t\"green\"\t\"\"\t\"查询消纳积分\"\t\"{\"\"accountCode\"\":\"\"131010601502988056\"\",\"\"tranTime\"\":1541896870850,\"\"encryptData\"\":\"\"d84aa9bdb3e479c64299200aa64065f6\"\",\"\"pageSize\"\":10,\"\"app_id\"\":\"\"M1523497402120\"\",\"\"pageNum\"\":1}\"\t\"1\"\t\"2018/11/14 08:28:50\"\t\"1\"\t\"2018/11/14 08:28:50\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00006ef299634859adfc4266f5598f1f\"\t\"green\"\t\"\"\t\"查询个人积分信息\"\t\"{\"\"accountCode\"\":\"\"131011801866652317\"\",\"\"tranTime\"\":1555640825804,\"\"encryptData\"\":\"\"8ff44f2ea1776ccb8d8fd924e34f81a4\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/4/25 21:23:07\"\t\"1\"\t\"2019/4/25 21:23:07\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00008144a0a9408495d29ecf0fc6000a\"\t\"green\"\t\"\"\t\"查询用户积分信息\"\t\"{\"\"accountCode\"\":\"\"131010401829386812\"\",\"\"tranTime\"\":1548393346263,\"\"encryptData\"\":\"\"fd38d26c9e7a365869358de7d21eaf02\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/2/2 21:57:18\"\t\"1\"\t\"2019/2/2 21:57:18\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00009200d4e04e898cd5e9e846696957\"\t\"green\"\t\"\"\t\"微站\"\t\"{\"\"accountCode\"\":\"\"131011701575252280\"\",\"\"tranTime\"\":1552617638183,\"\"encryptData\"\":\"\"c49b3a0a0d28a579d0e7b1c9b267e437\"\",\"\"prodType\"\":\"\"02\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/3/17 06:18:53\"\t\"1\"\t\"2019/3/17 06:18:53\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"000095dbeb214f16810fba7d8c41e7e9\"\t\"green\"\t\"\"\t\"查询个人积分信息\"\t\"{\"\"accountCode\"\":\"\"131023001926728828\"\",\"\"tranTime\"\":1550123506112,\"\"encryptData\"\":\"\"f0cfcf5d22cfec529193f297e8537bc6\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/3/5 19:44:10\"\t\"1\"\t\"2019/3/5 19:44:10\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00009d04da474abf8fa46efc6b7925bf\"\t\"green\"\t\"\"\t\"商品详情\"\t\"{\"\"tranTime\"\":1540401693674,\"\"encryptData\"\":\"\"0b4cd67ae4df301e290348214262cb95\"\",\"\"prodId\"\":\"\"854\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2018/11/5 10:26:45\"\t\"1\"\t\"2018/11/5 10:26:45\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n" +
                "\"00009f2542c1400ba5070e9024b41b13\"\t\"green\"\t\"\"\t\"礼品列表\"\t\"{\"\"accountCode\"\":\"\"031010901622107032\"\",\"\"tranTime\"\":1555640825921,\"\"encryptData\"\":\"\"9c16eac6450cdaf707ca972aed9902d1\"\",\"\"prodType\"\":\"\"01\"\",\"\"app_id\"\":\"\"M1523497402120\"\"}\"\t\"1\"\t\"2019/4/29 12:55:48\"\t\"1\"\t\"2019/4/29 12:55:48\"\t\"\"\t\"0\"\t\"1\"\t\"\"\t\"\"\t\"\"\t\"\"\n";
        String re1 = "";    //  1
        String re2 = ".*?";    // Non-greedy match on filler
        String re3 = "(\\{.*?\\})";    // Curly Braces 1
//        String re3 = "(^\\{.*\\}$)";
//        String re32 = "(^a.*b$)";
        Pattern p = Pattern.compile(re3, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m = p.matcher(txt);
        if (m.find()) {
            String s = m.group(1);
            System.out.print("(" + s.toString() + ")" + "\n");
        }
    }
}
